home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Online / hsc / source / hsclib / attrib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-02  |  9.2 KB  |  502 lines

  1. /*
  2.  * This source code is part of hsc, a html-preprocessor,
  3.  * Copyright (C) 1995-1997  Thomas Aglassinger
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  */
  20. /*
  21.  * hsclib/attribute.c
  22.  *
  23.  * hsc-variable funcs for hsc
  24.  *
  25.  * updated: 25-May-1997
  26.  * created:  2-Sep-1995
  27.  *
  28.  */
  29.  
  30. #define NOEXTERN_HSCLIB_ATTRIBUTE_H
  31.  
  32. #include "hsclib/inc_base.h"
  33.  
  34. #include "hsclib/eval.h"
  35. #include "hsclib/uri.h"
  36.  
  37. /*
  38.  *-------------------------------------
  39.  * debugging functions
  40.  *-------------------------------------
  41.  */
  42.  
  43. /*
  44.  * ptr_var
  45.  */
  46. static VOID prt_var(FILE * stream, APTR data)
  47. {
  48.     HSCATTR *var = (HSCATTR *) data;
  49.  
  50.     if (var)
  51.     {
  52.         int varquote = var->quote;
  53.         if (varquote == VQ_NO_QUOTE)
  54.             varquote = '.';
  55.  
  56.         /* name & type & macro_id */
  57.         fprintf(stream, "%s (type:%u,mci:%lu) ",
  58.                 var->name, var->vartype, var->macro_id);
  59.         /* text value */
  60.         if (var->text)
  61.             fprintf(stream, "cur:%c%s%c",
  62.                     var->quote, var->text, varquote);
  63.         else
  64.             fprintf(stream, "<NULL>");
  65.         fprintf(stream, " ");
  66.         /* default text */
  67.         if (var->deftext)
  68.             fprintf(stream, "def:%c%s%c",
  69.                     var->quote, var->deftext, varquote);
  70.         else
  71.             fprintf(stream, "<NULL>");
  72.         fprintf(stream, "\n");
  73.     }
  74.     else
  75.         fprintf(stream, "<NULL>\n");
  76. }
  77.  
  78. VOID prt_varlist(DLLIST * varlist, STRPTR title)
  79. {
  80.     fprintf(stderr, DHL "%s\n", title);
  81.     fprintf_dllist(stderr, varlist, prt_var);
  82. }
  83.  
  84. /*
  85.  *-------------------------------------
  86.  * constructor/destructor
  87.  *-------------------------------------
  88.  */
  89.  
  90. /*
  91.  * del_hscattr
  92.  *
  93.  * delete attribute
  94.  */
  95. VOID del_hscattr(APTR data)
  96. {
  97.     HSCATTR *var = (HSCATTR *) data;
  98.  
  99. #if DEBUG_ATTR
  100.     fprintf(stderr, DHL "   del_attr %s (mci=%d)\n",
  101.             var->name, var->macro_id);
  102. #endif
  103.  
  104.     /* release mem */
  105.     ufreestr(var->name);
  106.     ufreestr(var->deftext);
  107.     ufreestr(var->text);
  108.     ufreestr(var->enumstr);
  109.  
  110.     var->macro_id = 0;
  111.     var->varflag = 0;
  112.     var->vartype = VT_NONE;
  113.     var->quote = EOF;
  114.  
  115.     ufree(var);
  116.  
  117. }
  118.  
  119. /*
  120.  * new_hscattr
  121.  *
  122.  * alloc & init a new variable
  123.  */
  124. HSCATTR *new_hscattr(STRPTR newname)
  125. {
  126.     HSCATTR *newvar = (HSCATTR *) umalloc(sizeof(HSCATTR));
  127.  
  128. #if DEBUG_ATTR
  129.     fprintf(stderr, DHL "   new_attr %s\n", newname);
  130. #endif
  131.  
  132.     if (newvar)
  133.     {
  134.         /* init new varument item */
  135.         newvar->vartype = VT_NONE;
  136.         newvar->name = strclone(newname);
  137.         newvar->deftext = NULL;
  138.         newvar->text = NULL;
  139.         newvar->enumstr = NULL;
  140.         newvar->macro_id = 0;
  141.         if (upstrncmp(newname, PREFIX_HSCATTR, strlen(PREFIX_HSCATTR)))
  142.             newvar->varflag = 0;
  143.         else
  144.             newvar->varflag = VF_KEEP_QUOTES;
  145.         newvar->quote = VQ_NO_QUOTE;
  146.     }
  147.  
  148.     if (!(newvar->name))
  149.     {
  150.         del_hscattr((APTR) newvar);
  151.         newvar = NULL;
  152.     }
  153.  
  154.     return (newvar);
  155. }
  156.  
  157. /*
  158.  * cpy_hscattr
  159.  *
  160.  * create copy of an already existing attribute
  161.  */
  162. HSCATTR *cpy_hscattr(HSCATTR * oldvar)
  163. {
  164.     HSCATTR *newvar = new_hscattr(oldvar->name);
  165.  
  166.     if (newvar)
  167.     {
  168.         newvar->vartype = oldvar->vartype;
  169.         if (oldvar->deftext)
  170.             newvar->deftext = strclone(oldvar->deftext);
  171.         if (oldvar->text)
  172.             newvar->text = strclone(oldvar->text);
  173.         if (oldvar->enumstr)
  174.             newvar->enumstr = strclone(oldvar->enumstr);
  175.         newvar->macro_id = oldvar->macro_id;
  176.         newvar->varflag = oldvar->varflag;
  177.         newvar->quote = oldvar->quote;
  178.     }
  179.  
  180.     if (!(newvar->name))
  181.     {
  182.         del_hscattr((APTR) newvar);
  183.         newvar = NULL;
  184.     }
  185.  
  186.     return (newvar);
  187.  
  188. }
  189.  
  190. /*
  191.  * app_var
  192.  *
  193.  * append a new var to the var list AT BEGINNING
  194.  */
  195. HSCATTR *app_var(DLLIST * varlist, STRPTR newname)
  196. {
  197.     HSCATTR *var = new_hscattr(newname);
  198.     BOOL ok = FALSE;
  199.  
  200.     if (var)
  201.         ok = (ins_dlnode(varlist, varlist->first, (APTR) var) != NULL);
  202.  
  203.     if (!ok)
  204.     {
  205.         del_hscattr((APTR) var);
  206.         var = NULL;
  207.     }
  208.  
  209.     return (var);
  210. }
  211.  
  212. /*
  213.  *-------------------------------------
  214.  * search functions
  215.  *-------------------------------------
  216.  */
  217.  
  218. /*
  219.  * cmp_varname
  220.  *
  221.  * compares a var-name with the name
  222.  * of a HSCATTR-entry
  223.  */
  224. int cmp_varname(APTR cmpstr, APTR vardata)
  225. {
  226.     STRPTR varstr = NULL;
  227.  
  228.     if (vardata)
  229.     {
  230.         varstr = ((HSCATTR *) vardata)->name;
  231.     }
  232.  
  233.     if (varstr)
  234.     {
  235.         if (!upstrcmp((STRPTR) cmpstr, varstr))
  236.         {
  237.             return -1;
  238.         }
  239.         else
  240.         {
  241.             return 0;
  242.         }
  243.     }
  244.     else
  245.     {
  246.         return 0;
  247.     }
  248. }
  249.  
  250. /*
  251.  * find_attrnode
  252.  */
  253. DLNODE *find_attrnode(DLLIST * varlist, STRPTR name)
  254. {
  255.     DLNODE *nd = find_dlnode(varlist->first, (APTR) name, cmp_varname);
  256.  
  257.     return (nd);
  258. }
  259.  
  260. /*
  261.  * find_varname
  262.  */
  263. HSCATTR *find_varname(DLLIST * varlist, STRPTR name)
  264. {
  265.     DLNODE *nd = find_dlnode(varlist->first, (APTR) name, cmp_varname);
  266.     HSCATTR *var = NULL;
  267.  
  268.     if (nd)
  269.         var = (HSCATTR *) nd->data;
  270.  
  271.     return (var);
  272. }
  273.  
  274. /*
  275.  *-------------------------------------
  276.  * misc. functions
  277.  *-------------------------------------
  278.  */
  279.  
  280. /*
  281.  * set_vartext
  282.  *
  283.  * set value of a var
  284.  *
  285.  * params: var......var to set
  286.  *         newtext..new text to set
  287.  * result: value of new text set
  288.  * errors: return NULL if out of mem, display error message
  289.  */
  290. STRPTR set_vartext(HSCATTR * var, STRPTR newtext)
  291. {
  292.     if (!var)
  293.     {
  294.         panic("NULL attribute detected");
  295.     }
  296.  
  297.     if (newtext != var->text)
  298.     {
  299.         ufreestr(var->text);
  300.         var->text = NULL;
  301.         if (newtext)
  302.         {
  303.             var->text = strclone(newtext);
  304.         }
  305.     }
  306.  
  307.     return (var->text);
  308. }
  309.  
  310. /*
  311.  * set_varbool
  312.  *
  313.  * set value of a boolean attr
  314.  *
  315.  * params: var....var to set
  316.  *         value..new value to set
  317.  * result: value
  318.  * errors: if out of mem, display error message
  319.  */
  320. BOOL set_varbool(HSCATTR * attr, BOOL value)
  321. {
  322.     if (value)
  323.         set_vartext(attr, attr->name);
  324.     else
  325.         set_vartext(attr, "");
  326.  
  327.     return (value);
  328. }
  329.  
  330. /*
  331.  * clr_vartext
  332.  *
  333.  * clear or reset var to default text
  334.  *
  335.  * params: var..var to clear
  336.  * result: TRUE, if new value set correctly
  337.  */
  338. BOOL clr_vartext(HSCATTR * var)
  339. {
  340.     BOOL ok = TRUE;
  341.  
  342.     if (var->deftext)
  343.     {
  344.         if (!(set_vartext(var, var->deftext)))
  345.             ok = FALSE;
  346.     }
  347.     else
  348.     {
  349.         ufreestr(var->text);
  350.         var->text = NULL;
  351.     }
  352.  
  353.     return (ok);
  354. }
  355.  
  356. /*
  357.  * clr_attrdef
  358.  *
  359.  * clear attributes default text
  360.  *
  361.  * params: attr..attr to clear
  362.  */
  363. VOID clr_attrdef(HSCATTR * attr)
  364. {
  365.     ufreestr(attr->deftext);
  366.     attr->deftext = NULL;
  367. }
  368.  
  369. /*
  370.  * clr_varlist
  371.  *
  372.  * clear all vars to default text
  373.  *
  374.  * params: varlist..varlist to clear
  375.  */
  376. BOOL clr_varlist(DLLIST * varlist)
  377. {
  378.     DLNODE *nd = varlist->first;
  379.     BOOL ok = TRUE;
  380.  
  381.     while (nd && ok)
  382.     {
  383.         ok &= clr_vartext((HSCATTR *) nd->data);
  384.         nd = nd->next;
  385.     }
  386.  
  387.     return (ok);
  388. }
  389.  
  390. /*
  391.  * clr_varlist_bool
  392.  *
  393.  * set all "empty" (=NULL) boolean attributes of
  394.  * an attr list to FALSE
  395.  *
  396.  * params: varlist..varlist to process
  397.  */
  398. VOID clr_varlist_bool(DLLIST * varlist)
  399. {
  400.     DLNODE *nd = varlist->first;
  401.     BOOL ok = TRUE;
  402.  
  403.     while (nd && ok)
  404.     {
  405.         HSCATTR *attr = (HSCATTR *) nd->data;
  406.  
  407.         if ((attr->vartype == VT_BOOL) && !(attr->text))
  408.             set_varbool(attr, FALSE);
  409.  
  410.         nd = nd->next;
  411.     }
  412. }
  413.  
  414. /*
  415.  * get_vartext_byname: get text value of a var
  416.  *
  417.  *
  418.  */
  419. STRPTR get_vartext_byname(DLLIST * varlist, STRPTR name)
  420. {
  421.     HSCATTR *var = find_varname(varlist, name);
  422.     STRPTR vartext = NULL;
  423.  
  424.     if (var)
  425.         vartext = var->text;
  426.  
  427.     return (vartext);
  428. }
  429.  
  430. /*
  431.  * get_vartext: get text value of a var
  432.  *
  433.  */
  434. STRPTR get_vartext(HSCATTR * var)
  435. {
  436.     STRPTR text = NULL;
  437.  
  438.     if (var)
  439.         text = var->text;
  440.  
  441.     return (text);
  442. }
  443.  
  444. /*
  445.  * get_varbool: get value of a boolean attr
  446.  */
  447. BOOL get_varbool(HSCATTR * attr)
  448. {
  449.     BOOL set = FALSE;
  450.     if (attr && (attr->text[0]))
  451.         set = TRUE;
  452.  
  453.     return (set);
  454. }
  455.  
  456. /*
  457.  * get_varbool_byname: get value of a boolean attr
  458.  */
  459. BOOL get_varbool_byname(DLLIST * varlist, STRPTR name)
  460. {
  461.     HSCATTR *var = find_varname(varlist, name);
  462.  
  463.     return (get_varbool(var));
  464. }
  465.  
  466. /*
  467.  * get_varnum: get value of a numeric attr
  468.  */
  469. LONG get_varnum(HSCATTR * attr)
  470. {
  471.     LONG num = 0;
  472.     if (attr && (attr->text) && (attr->text[0]))
  473.         str2long(attr->text, &num);
  474.  
  475.     return (num);
  476. }
  477.  
  478. /*
  479.  * get_varnum_byname: get value of a numeric attr
  480.  */
  481. LONG get_varnum_byname(DLLIST * varlist, STRPTR name)
  482. {
  483.     HSCATTR *var = find_varname(varlist, name);
  484.  
  485.     return (get_varnum(var));
  486. }
  487.  
  488. /*
  489.  * get_vardeftext: get default text value of a var
  490.  *
  491.  */
  492. STRPTR get_vardeftext(HSCATTR * var)
  493. {
  494.     STRPTR deftext = NULL;
  495.  
  496.     if (var)
  497.         deftext = var->deftext;
  498.  
  499.     return (deftext);
  500. }
  501.  
  502.